package com.speedricemanager.activity.home.registermodepart.presenter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jbm.core.iface.IAcross;
import com.jbm.jsm.iface.TaskMouldEntity;
import com.jbm.jsm.iface.TaskMouldRequireEntity;
import com.speedricemanager.R;
import com.speedricemanager.activity.home.fragmenadapter.SpnnerBaseadadianfu;
import com.speedricemanager.activity.home.intface.IntegerFaceOr;
import com.speedricemanager.activity.home.registermodepart.mode.RegisterModePartMode;
import com.speedricemanager.activity.home.registermodepart.view.RegisterModePart_Inter;
import com.speedricemanager.interfaces.DialogSuccess;
import com.speedricemanager.other.base.BaseAty;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Created by 李征 on 2017/12/18.
 */

public class RegisterModePartPersenter implements RegisterModePartPersenter_Inter {
    private Context context;
    private RegisterModePart_Inter registerModePart_inter;
    private RegisterModePartMode registerModePartMode;
    private ArrayList<HashMap<String,Object>> arrayList;
    ArrayList<HashMap<String ,Object>> arrayListid;
    ArrayList<HashMap<String ,String>>arrfid;
    private TaskMouldEntity ta;
    int b;
    int s=0;
    int tags;
    int bid;
    int sid=0;
    int tagsid;
    String str[]={"修改条件","修改佣金","删除条件"};
    public RegisterModePartPersenter(Context context, RegisterModePart_Inter registerModePart_inter) {
        this.context = context;
        this.registerModePart_inter = registerModePart_inter;
        registerModePartMode=new RegisterModePartMode();
        arrayList=new ArrayList<>();
        arrayListid=new ArrayList<>();
        arrfid=new ArrayList<>();
    }
    /**
     * 获取模板详情
     */
    @Override
    public void getMoluldInfo() {
        ((BaseAty)context).onLoad();
        registerModePartMode.getMoluldInfo(registerModePart_inter.getTmid(), new IntegerFaceOr() {
            @Override
            public void getSucces(Object o) {
                ((BaseAty)context).loadDismiss();
                 ta=(TaskMouldEntity)o;
                registerModePart_inter.telayoutaddtioajian().removeAllViews();
                registerModePart_inter.layoutaddid().removeAllViews();

                if (ta.getPlatTypeS()!=null){
                    registerModePart_inter.dtepingtai().setText(ta.getPlatTypeS());
                }
                if (ta.getTaskEnvironment()!=null){
                    String task;
                    if (ta.getTaskEnvironment()==1){
                        task="电脑";
                    }else if (ta.getTaskEnvironment()==2){
                        task="手机";
                    }else {
                        task="无限制";
                    }
                    registerModePart_inter.dtehuanjing().setText(task);}
                if (ta.getsSatisfy()!=null){
                    registerModePart_inter. jdrbzj().setText(ta.getsSatisfy()+"");}
                if (ta.getPerUserpay()!=null){
                    registerModePart_inter.dtedianzikuan().setText(ta.getPerUserpay()+"");}
                if (ta.getUserpayBelong()!=null){
                    String user = null;
                    if (ta.getUserpayBelong()==1){
                        user="接单人";
                    }else    if (ta.getUserpayBelong()==2){
                        user="发单人";
                    }
                    registerModePart_inter. dtedianfukuan().setText(user);}
                if (ta.getRequirement()!=null){
                    registerModePart_inter.dtejiaodanyaoqiu().setText(ta.getRequirement());}
                if (ta.getDoFlow()!=null){
                    registerModePart_inter.dtejiaodanliucheng().setText(ta.getDoFlow());}
                if (ta.getComment()!=null){
                    registerModePart_inter.dtejiedantiaojian().setText(ta.getComment());}
                if (ta.getStartDate()!=null){
                    registerModePart_inter.shezirenwushijian().setText(getTime(ta.getStartDate())+"        "+getTime(ta.getEndDate())+"");}
                if (ta.getStartDate()!=null){
                    registerModePart_inter.startime().setText(getTime(ta.getStartDate()));
                }
                if (ta.getEndDate()!=null){
                    registerModePart_inter.enttime().setText(getTime(ta.getEndDate()));}
                if (ta.getRental()!=null){
                    registerModePart_inter.dtezujin().setText(ta.getRental()+"");}
                if (ta.getMainAccount()!=null){
                    registerModePart_inter.dtezhuhaobaozhengjiin().setText(ta.getMainAccount()+"");}

                if (ta.getTmName()!=null){
                    registerModePart_inter. mobanname().setText(ta.getTmName());}

                if (ta.getStatus()==3){
                    registerModePart_inter.tongyishenqing().setVisibility(View.GONE);
                    registerModePart_inter.jujueshenqing().setVisibility(View.GONE);
                    registerModePart_inter.layoutbaozhengjin().setVisibility(View.GONE);
                    registerModePart_inter.startime().setEnabled(false);
                    registerModePart_inter.enttime().setEnabled(false);
                }
                if (ta.getStatus()!=1){
//                    registerModePart_inter.layoutbaozhengjin().setVisibility(View.GONE);
                    registerModePart_inter.tongyishenqing().setVisibility(View.GONE);
                    registerModePart_inter.jujueshenqing().setVisibility(View.GONE);
                    registerModePart_inter.startime().setEnabled(false);
                    registerModePart_inter.enttime().setEnabled(false);
                    registerModePart_inter.zhuhbaozhengjin().setEnabled(false);
                    registerModePart_inter.fuhaobangzhengjin().setEnabled(false);
                    registerModePart_inter.zhuhbaozhengjin().setFocusable(true);
                    registerModePart_inter.fuhaobangzhengjin().setFocusable(true);
                    registerModePart_inter.zhuhbaozhengjin().setText("主账号保证金:"+ta.getMainDeposit());
                    registerModePart_inter.fuhaobangzhengjin().setText("付账号保证金:"+ta.getViceDeposit());
                }
                if (ta.getStatus()==5){
                    registerModePart_inter.tiqianjieshu().setVisibility(View.VISIBLE);
                }

                if (ta.getStatus()==1){
                    registerModePart_inter.shenfen().setVisibility(View.VISIBLE);
                    registerModePart_inter.buqujifei().setVisibility(View.VISIBLE);
                    registerModePart_inter.buqujdyq().setVisibility(View.VISIBLE);
                    registerModePart_inter. buqujdlc().setVisibility(View.VISIBLE);
                    registerModePart_inter.buqujdtj().setVisibility(View.VISIBLE);
                    registerModePart_inter.buquzj().setVisibility(View.VISIBLE);
                }
if (ta.getCreateUser()!=null){
    String str = "";
    switch (ta.getCreateUser().getfId()){
        case 1:
            str="紫马";
            break;
        case 2:
            str="橙马";
            break;
        case 3:
            str="黄马";
            break;
        case 4:
            str="红马";
            break;
        case 5:
            str="蓝马";
            break;


    }
    registerModePart_inter.shenfen().setText(str);
}
               //编辑交单条件
                getRequireList(ta);
                //获取副账号id
                getRequireListids(ta);
            }

            @Override
            public void getError(Object o) {
                ((BaseAty)context).loadDismiss();
                TaskMouldEntity ta=(TaskMouldEntity)o;
                ((BaseAty)context).errorAlert(ta.m_info);
            }
        });
    }

    String dianzhikuany = null; String jiedanyaoqiuy= null;  String jiedantiaojiany= null; String jiedanliuchengy= null; String zujiny= null;
    /**
     * 同意或拒绝模板
     */
    @Override
    public void AuditTaskMoulds() {
        ArrayList<String> arr=new ArrayList<>();
        String dianzhikuan = null; String jiedanyaoqiu = null;  String jiedantiaojian= null; String jiedanliucheng= null; String zujin= null;

        if (!registerModePart_inter.dtedianzikuaner().getText().toString().equals("")){
            dianzhikuan="修改垫资款:"+registerModePart_inter.dtedianzikuaner().getText().toString();
            arr.add(dianzhikuan);
            dianzhikuany=registerModePart_inter.dtedianzikuaner().getText().toString();
        }else {
            dianzhikuany=registerModePart_inter.dtedianzikuan().getText().toString();
        }
        if (!registerModePart_inter.dtejiaodanyaoqiuer().getText().toString().equals("")){
            jiedanyaoqiu="修改接单要求:"+registerModePart_inter.dtejiaodanyaoqiuer().getText().toString();
            arr.add(jiedanyaoqiu);
            jiedanyaoqiuy=registerModePart_inter.dtejiaodanyaoqiuer().getText().toString();
        }else {

            jiedanyaoqiuy=registerModePart_inter.dtejiaodanyaoqiu().getText().toString();

        }
        if (!registerModePart_inter.dtejiaodanliuchenger().getText().toString().equals("")){
            jiedanliucheng="修改接单流程:"+registerModePart_inter.dtejiaodanliuchenger().getText().toString();
            arr.add(jiedanliucheng);
            jiedanliuchengy=registerModePart_inter.dtejiaodanliuchenger().getText().toString();
        }else {
            jiedanliuchengy=registerModePart_inter.dtejiaodanliucheng().getText().toString();
        }



        if (!registerModePart_inter.dtejiedantiaojianer().getText().toString().equals("")){
            jiedantiaojian="修改接单条件:"+registerModePart_inter.dtejiedantiaojianer().getText().toString();
            arr.add(jiedantiaojian);
            jiedantiaojiany=registerModePart_inter.dtejiedantiaojianer().getText().toString();
        }else {
            jiedantiaojiany=registerModePart_inter.dtejiedantiaojian().getText().toString();
        }


        if (!registerModePart_inter.dtezujiner().getText().toString().equals("")){
            zujin="修改租金:"+registerModePart_inter.dtezujiner().getText().toString();
            arr.add(zujin);
            zujiny=registerModePart_inter.dtezujiner().getText().toString();
        }else {
            zujiny=registerModePart_inter.dtezujin().getText().toString();
        }
        for (int j = 0; j <arrayList.size() ; j++) {
            if (!((TextView)arrayList.get(j).get("tiaojiannameer")).getText().toString().equals("")){
            arr.add("修改交单条件"+((TextView)arrayList.get(j).get("tiaojiannameer")).getText().toString());}
            if (!((TextView)arrayList.get(j).get("dyongjintexter")).getText().toString().equals("")){
            arr.add("修改交单租金:"+((TextView) arrayList.get(j).get("dyongjintexter")).getText().toString());}
        }
        if (arr.size()<=0){
            arr.add("是否要通过没有修改过的模板?");
        }



        final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(context);
        View view = View.inflate(context, R.layout.rejisterlistview, null);
        rxDialogSureCancel.setContentView(view);
        ListView listview = view.findViewById(R.id.listview);
        TextView canceldia = view.findViewById(R.id.canceldia);
        TextView commitdia = view.findViewById(R.id.commitdia);
        listview.setAdapter(new SpnnerBaseadadianfu(context,arr));
        canceldia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogSureCancel.dismiss();
            }
        });
        commitdia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((BaseAty)context).onLoad();
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                Date bodata=null;
                Date bodata1=null;
                try {
                    bodata = df.parse(registerModePart_inter.startime().getText().toString());
                    bodata1 = df.parse(registerModePart_inter.enttime().getText().toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
        registerModePartMode.AuditTaskMoulds(ta.getTmId(), registerModePart_inter.gettagstu(),registerModePart_inter.getjujue(), dianzhikuany, jiedanyaoqiuy, jiedanliuchengy, jiedantiaojiany,bodata ,bodata1 , zujiny, get(), registerModePart_inter.zhuhbaozhengjin().getText().toString(),  registerModePart_inter.fuhaobangzhengjin().getText().toString(),  getrequireList(), new IntegerFaceOr() {
            @Override
            public void getSucces(Object o) {
                ((BaseAty)context).loadDismiss();
                IAcross iAcross=(IAcross)o;
                ((BaseAty)context).finish();
            }
            @Override
            public void getError(Object o) {
                ((BaseAty)context).loadDismiss();
                IAcross iAcross=(IAcross)o;
                ((BaseAty)context).errorAlert(iAcross.m_info);

            }
        });
                rxDialogSureCancel.dismiss();
            }
        });
        rxDialogSureCancel.show();








    }
    /**
     * 结束模板
     */
    @Override
    public void overMould() {
        ((BaseAty)context).onLoad();
        registerModePartMode.overMould(ta.getTmId(), new IntegerFaceOr() {
            @Override
            public void getSucces(Object o) {
                ((BaseAty)context).loadDismiss();
                IAcross iAcross=(IAcross)o;
                ((BaseAty)context).finish();
            }

            @Override
            public void getError(Object o) {
                ((BaseAty)context).loadDismiss();
                IAcross iAcross=(IAcross)o;
                ((BaseAty)context).errorAlert(iAcross.m_info);
            }
        });



    }
    public String getTime(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(date);
    }
    public void getRequireList(  TaskMouldEntity ta){
        b=0;

        if (ta.getRequireList()!=null){
            Button butt = null;
            for (int i = 0; i < ta.getRequireList().size(); i++) {
                final HashMap<String,Object> hashMap=new HashMap<String, Object>();
              final   View vie= View.inflate(context, R.layout.registerresult,null);
                if (tags==0){
                    vie.setId(s);
                    tags=1;
                }else {
                    vie.setId(++s);
                }
                registerModePart_inter.telayoutaddtioajian().addView(vie);
                ++b;
                TextView tiaojian = (TextView) vie.findViewById(R.id.tejiaodantiaojian);
                TextView tiaojianer = (TextView) vie.findViewById(R.id.tejiaodantiaojianer);
                TextView tiaojiannameer = (TextView) vie.findViewById(R.id.dtejiaodantiaojianer);
                TextView tiaojianname = (TextView) vie.findViewById(R.id.dtejiaodantiaojian);
                final LinearLayout layoutjdan=(LinearLayout)  vie.findViewById(R.id.layoutjdan);
                TextView teyongjiin = (TextView) vie.findViewById(R.id.teyongjiin);
                TextView teyongtext = (TextView) vie.findViewById(R.id.teyongtext);
                TextView teyonjinr = (TextView) vie.findViewById(R.id.teyonjinr);
                TextView dyongjintexter = (TextView) vie.findViewById(R.id.dyongjintexter);
                final LinearLayout layoutyj=(LinearLayout)  vie.findViewById(R.id.layoutyj);
                tiaojian .setText("交单条件"+b+":    ");
                teyongjiin.setText("佣   金"+b+":    ");
                if (ta.getRequireList().get(i).getRequirement()!=null){
                    tiaojianname.setText(ta.getRequireList().get(i).getRequirement()+"");
                }
                if (ta.getRequireList().get(i).getAmount()!=null){
                    teyongtext.setText(ta.getRequireList().get(i).getAmount()+"");
                }else {
                    teyongtext.setText("0");
                }

                butt = (Button) vie.findViewById(R.id.buqujifei);
                if (ta.getStatus()==1){
                    butt.setVisibility(View.VISIBLE);
                }
                hashMap.put("tiaojian",tiaojian);
                hashMap.put("tiaojianname",tiaojianname);
                hashMap.put("tiaojianer",tiaojianer);
                hashMap.put("tiaojiannameer",tiaojiannameer);
                hashMap.put("layoutjdan",layoutjdan);
                hashMap.put("teyongjiin",teyongjiin);
                hashMap.put("teyongtext",teyongtext);
                hashMap.put("teyonjinr",teyonjinr);
                hashMap.put("dyongjintexter",dyongjintexter);
                hashMap.put("layoutyj",layoutyj);
                hashMap.put("butt",butt);
                hashMap.put("vie",vie);
                hashMap.put("vid",vie.getId());
                hashMap.put("tmrid",ta.getRequireList().get(i).getTmrId());
                hashMap.put("Amount",ta.getRequireList().get(i).getAmount());
                arrayList.add(hashMap);
                butt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (int i = 0; i < arrayList.size(); i++) {
                            if ((int)arrayList.get(i).get("vid")== vie.getId()){
                                final int finalI = i;
                                ((BaseAty)context).getLoadDialog(str, new IntegerFaceOr() {
                                    @Override
                                    public void getSucces(Object o) {
                                     int tag=(int)o;
                                        switch (tag){
                                            case 0:
                                ((BaseAty)context).editAlert("确定要修改"+((TextView)arrayList.get(finalI).get("tiaojian")).getText().toString(), new DialogSuccess() {
                                    @Override
                                    public void option(String password) {
                                        HashMap<String ,String> hashMap1=new HashMap<String, String>();
                                        layoutjdan.setVisibility(View.VISIBLE);
                                        ((TextView)arrayList.get(finalI).get("tiaojianer")).setText(((TextView)arrayList.get(finalI).get("tiaojian")).getText().toString());
                                        ((TextView)arrayList.get(finalI).get("tiaojiannameer")).setText(password+"");
                                    }
                                });
                                                break;
                                            case 1:
                                                ((BaseAty)context).editAlert("确定要修改"+((TextView)arrayList.get(finalI).get("teyongjiin")).getText().toString(), new DialogSuccess() {
                                                    @Override
                                                    public void option(String password) {
                                                        HashMap<String ,String> hashMap1=new HashMap<String, String>();
                                                        layoutyj.setVisibility(View.VISIBLE);
                                                        ((TextView)arrayList.get(finalI).get("teyonjinr")).setText(((TextView)arrayList.get(finalI).get("teyongjiin")).getText().toString());
                                                        BigDecimal bd = new BigDecimal(password);
                                                        ((TextView)arrayList.get(finalI).get("dyongjintexter")).setText(""+bd.floatValue());
                                                    }
                                                });
                                                break;
                                            case 2:
                                                registerModePart_inter.telayoutaddtioajian().removeView((View) arrayList.get(finalI).get("vie"));
                                                arrayList.remove(finalI);
                                                break;
                                        }
                                    }
                                    @Override
                                    public void getError(Object o) {

                                    }
                                });
                            }
                        }
                    }
                });
            }


        }
    }
    public  List<TaskMouldRequireEntity>  getrequireList(){
        List<TaskMouldRequireEntity>  getrequireList=new ArrayList<>();
        for (int j = 0; j <arrayList.size() ; j++) {
            TaskMouldRequireEntity ta=new TaskMouldRequireEntity();
            if (((TextView)arrayList.get(j).get("tiaojiannameer")).getText().toString().equals("")){
                ta.setRequirement(((TextView)arrayList.get(j).get("tiaojianname")).getText().toString());
                ta.setRequirementOld(((TextView)arrayList.get(j).get("tiaojianname")).getText().toString());
            }else {
                ta.setRequirement(((TextView)arrayList.get(j).get("tiaojiannameer")).getText().toString());
                ta.setRequirementOld(((TextView)arrayList.get(j).get("tiaojiannameer")).getText().toString());
            }
        if (((TextView)arrayList.get(j).get("dyongjintexter")).getText().toString().equals("")){
            ta.setAmount(new BigDecimal(((TextView)arrayList.get(j).get("teyongtext")).getText().toString().trim()));
            ta.setAmountOld(new BigDecimal(((TextView)arrayList.get(j).get("teyongtext")).getText().toString().trim()));
        }else {
            String te = ((TextView) arrayList.get(j).get("dyongjintexter")).getText().toString();
            BigDecimal big = new BigDecimal(te);
            ta.setAmount(big);
            ta.setAmountOld(big);
        }
            ta.setTmrId((int)arrayList.get(j).get("tmrid"));
            getrequireList.add(ta);
        }
        return getrequireList;

    }
    public void getRequireListids(TaskMouldEntity ta){
        bid=0;


        if (ta.getViceAccountList()!=null){
            Button buqufzh = null;
            for (int i = 0; i < ta.getViceAccountList().size(); i++) {
                HashMap<String,Object> hashMap=new HashMap<String, Object>();
                final View vid= View.inflate(context, R.layout.registerortemplatetiaojian,null);
                if (tagsid==0){
                    vid.setId(sid);
                    tagsid=1;
                }else {
                    vid.setId(++sid);
                }
                registerModePart_inter.layoutaddid().addView(vid);
                ++bid;
                TextView fuhao = (TextView) vid.findViewById(R.id.fuhao);
                TextView fuhaoer = (TextView) vid.findViewById(R.id.fuhaoer);
                TextView dtefuhaobaozhengjiner = (TextView) vid.findViewById(R.id.dtefuhaobaozhengjiner);
                LinearLayout layoutfuhoa=(LinearLayout)  vid.findViewById(R.id.layoutfuhoa);
                fuhao .setText("副账号"+bid+":");
                TextView dtefuhaobaozhengjin = (TextView) vid.findViewById(R.id.dtefuhaobaozhengjin);
                if (ta.getViceAccountList().get(i).getAccount()!=null){
                    dtefuhaobaozhengjin.setText(ta.getViceAccountList().get(i).getAccount()+"  "+ta.getViceAccountList().get(i).getNickname());
                }
                buqufzh = (Button) vid.findViewById(R.id.buqufzh);
                if (ta.getStatus()==1){
                    buqufzh.setVisibility(View.VISIBLE);
                }
                hashMap.put("tiaojian",fuhao);
                hashMap.put("tiaojianname",dtefuhaobaozhengjin);
                hashMap.put("tiaojianer",fuhaoer);
                hashMap.put("tiaojiannameer",dtefuhaobaozhengjiner);
                hashMap.put("layoutjdan",layoutfuhoa);
                hashMap.put("butt",buqufzh);
                hashMap.put("v",vid);
                hashMap.put("vid",vid.getId());
                hashMap.put("id", ta.getViceAccountList().get(i).getUserId());
                arrayListid.add(hashMap);
                buqufzh.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (int i = 0; i < arrayListid.size(); i++) {
                            if ((int)arrayListid.get(i).get("vid")== vid.getId()){
                                registerModePart_inter.layoutaddid().removeView((View)arrayListid.get(i).get("v"));
                                arrayListid.remove(i);
                            }
                        }
                    }
                });
            }


        }
    }
    public String get(){
        StringBuffer str=new StringBuffer();
        if (arrayListid.size()<=0){
        }else {
        for (int i = 0; i < arrayListid.size(); i++) {
            str.append(",").append((int)arrayListid.get(i).get("id"));
        }
        str.append(",");
        }



        return str.toString();



    }
}
